package org.spring.springboot.util;

import java.math.BigDecimal;

public class NumberUtil {
	public static int parseInt(Object obj){
		if(obj == null) return 0;
		int result = 0;
		try {
			result = (int)Double.parseDouble(obj.toString());
		} catch (Exception e) {
			result = 0;
		}
		return result;
	}
	
	public static int parseInt(Object obj, int defaultValue){
		if(obj == null) return defaultValue;
		int result = defaultValue;
		try {
			result = (int)Double.parseDouble(obj.toString());
		} catch (Exception e) {
			result = defaultValue;
		}
		return result;
	}
	
	public static double parseDouble(Object obj){
		if(obj == null) return 0;
		double result = 0;
		try {
			result = Double.parseDouble(obj.toString());
		} catch (Exception e) {
			result = 0;
		}
		return result;
	}
	
	public static double parseDouble(Object obj, double defaultValue){
		if(obj == null) return defaultValue;
		double result = defaultValue;
		try {
			result = Double.parseDouble(obj.toString());
		} catch (Exception e) {
			result = defaultValue;
		}
		return result;
	}
	public static float parseFloat(Object obj){
		if(obj == null) return 0;
		float result = 0;
		try {
			result = Float.parseFloat(obj.toString());
		} catch (Exception e) {
			result = 0;
		}
		return result;
	}
	
	public static double parseDouble(Object obj, float defaultValue){
		if(obj == null) return defaultValue;
		float result = defaultValue;
		try {
			result = Float.parseFloat(obj.toString());
		} catch (Exception e) {
			result = defaultValue;
		}
		return result;
	}
	
	
	public static long parseLong(Object obj){
		if(obj == null) return 0;
		long result = 0;
		try {
			result = Long.parseLong(obj.toString());
		} catch (Exception e) {
			result = 0;
		}
		return result;
	}
	
	public static long parseLong(Object obj, long defaultValue){
		if(obj == null) return defaultValue;
		long result = defaultValue;
		try {
			result = Long.parseLong(obj.toString());
		} catch (Exception e) {
			result = defaultValue;
		}
		return result;
	}
	
	public static String getDisplayValue(double value, int scale) {
		String result = String.format("%." + scale + "f", value);
		return BigDecimal.valueOf(Double.parseDouble(result)).stripTrailingZeros().toPlainString();
	}
	
}
